#!/bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground

# 函数：在线安装 ClickHouse
function installClickHouseOnline() {
    local version=$1
    local base_url="https://packages.clickhouse.com/rpm/stable/"

    # 定义 RPM 包数组
    declare -a packages=("clickhouse-client" "clickhouse-common-static" "clickhouse-keeper" "clickhouse-server")

    # 创建临时目录用于存放下载的包
    local temp_dir=$(mktemp -d)
    trap 'rm -rf "$temp_dir"' EXIT

    for pkg in "${packages[@]}"; do
        echo "正在下载 ${pkg}-${version}.x86_64.rpm ..."
        wget --no-check-certificate "${base_url}${pkg}-${version}.x86_64.rpm" -P "$temp_dir"
        if [ $? -ne 0 ]; then
            echo "下载 ${pkg}-${version}.x86_64.rpm 失败"
            return 1
        fi
    done

    # 安装 RPM 包
    for pkg in "${packages[@]}"; do
        echo "正在安装 ${pkg}-${version}.x86_64.rpm ..."
        rpm -ivh "$temp_dir/${pkg}-${version}.x86_64.rpm"
        if [ $? -ne 0 ]; then
            echo "安装 ${pkg}-${version}.x86_64.rpm 失败"
            return 1
        fi
    done

    echo "ClickHouse 安装完成"
}

# 函数：离线安装 ClickHouse
function installClickHouseOffline() {
    local version=$1
    local offline_dir="$PLAY_HOME/frames/clickhouse-$version"

    # 定义 RPM 包数组
    declare -a packages=("clickhouse-common-static" "clickhouse-client" "clickhouse-server" "clickhouse-keeper")

    # 读取密码
    local password
    password=$(grep "clickhouse-admin-user-password" "$PLAY_HOME/configs.txt" | cut -d ' ' -f 2)

    echo "install..."

    # 安装 RPM 包
    for pkg in "${packages[@]}"; do
        echo "正在安装 ${pkg}-${version}.x86_64.rpm ..."
        expect -c "
set timeout 20
spawn rpm -ivh \"$offline_dir/${pkg}-${version}.x86_64.rpm\"
expect {
    \"Enter password for the default user:\" {
        send \"$password\r\"
        exp_continue
    }
    \"Preparing...\" {
        exp_continue
    }
    eof
}
"
        if [ $? -ne 0 ]; then
            echo "安装 ${pkg}-${version}.x86_64.rpm 失败"
            return 1
        fi
    done

    echo "ClickHouse 离线安装完成"
}

# 主程序
function main() {
    # 从配置文件中获取 ClickHouse 安装信息
    clickhouseInfo=$(egrep "^clickhouse" $PLAY_HOME/frames.txt)

    ck=$(echo $clickhouseInfo | cut -d " " -f1)
    isInstall=$(echo $clickhouseInfo | cut -d " " -f2)
    installNode=$(echo $clickhouseInfo | cut -d " " -f3)
    masterNode=$(echo $clickhouseInfo | cut -d " " -f4)
    node=$(hostname)

    # 提取版本号
    clickhouseVersion=$(echo "$ck" | grep -oP '(?<=-)[0-9\.]+(?=(-|\.|$))')
    if [ -z "$clickhouseVersion" ]; then
        echo "无法从 $ck 中提取版本号。"
        return 1
    fi

    echo "$clickhouseVersion"

    # 检查是否需要安装
    if [[ $isInstall = "true" && $installNode =~ $node ]];then
        # read -p "是否在线安装 ClickHouse? (y/n): " onlineInstall
        # echo "您输入的是: $onlineInstall"
        # if [[ $onlineInstall == "y" ]]; then
        #     installClickHouseOnline "$clickhouseVersion"
        # else
        #     installClickHouseOffline "$clickhouseVersion"
        # fi
        installClickHouseOffline "$clickhouseVersion"
    else
        echo "ClickHouse 不允许安装在当前节点，请检查配置文件！"
        exit 1
    fi
}

# 执行主程序
main